package com.es.www.kyl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.es.www.kyl.bean.req.web.CategoryAddReq;
import com.es.www.kyl.bean.req.web.CategoryReq;
import com.es.www.kyl.bean.req.web.CategoryUpdateReq;
import com.es.www.kyl.bean.res.web.CategoryListVO;
import com.es.www.kyl.bean.res.web.CategoryVO;
import com.es.www.kyl.domain.Category;
import com.es.www.kyl.domain.Question;
import com.es.www.kyl.exception.ResException;
import com.es.www.kyl.mapper.CategoryMapper;
import com.es.www.kyl.mapper.QuestionMapper;
import com.es.www.kyl.service.CategoryService;
import com.es.www.kyl.service.mapstruct.CategoryMapConstruct;
import com.es.www.kyl.util.UserContextHolder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 * @createDate 2023-04-01 14:12:08
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


	private final CategoryMapper categoryMapper;
	private final QuestionMapper questionMapper;


	@Override
	public PageInfo<CategoryListVO> pageQueryCategoryList(CategoryReq categoryReq) {
		PageHelper.startPage(categoryReq.getPageNum(), categoryReq.getPageSize());
		List<CategoryListVO> list = categoryMapper.selectCategoryList(categoryReq);
		list.forEach(category -> {
			// 查询题目数量
		Long questionNum = questionMapper.selectCount(new LambdaQueryWrapper<Question>()
				.eq(Question::getCategoryId, category.getId()));
			category.setQuestionNum(Integer.valueOf(questionNum.toString()));
		});
		return new PageInfo<>(list);
	}

	@Override
	public List<CategoryVO> getTopCategoryList() {
		List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
				.eq(Category::getParentId, 0)
				.orderByAsc(Category::getSort));
		return CategoryMapConstruct.INSTANCE.toCategoryVOList(categories);
	}

	@Override
	public void addCategory(CategoryAddReq categoryAddReq) {
		Category category = new Category();
		category.setName(categoryAddReq.getCategoryName());
		category.setParentId(categoryAddReq.getParentId());
		category.setDescription(categoryAddReq.getDescription());
		category.setStyle(categoryAddReq.getStyle());
		category.setCreateBy(UserContextHolder.getUserId());
		categoryMapper.insertSelective(category);
	}

	@Override
	public void updateCategory(CategoryUpdateReq categoryUpdateReq) {
		Category category = categoryMapper.selectById(categoryUpdateReq.getCategoryId());
		if (Objects.isNull(category)) {
			throw new ResException("分类不存在");
		}
		// 如果是一级分类，判断是否有子分类
		if (category.getParentId() == 0) {
			Long count = categoryMapper.selectCount(new LambdaQueryWrapper<Category>()
					.eq(Category::getParentId, category.getId()));
			if (count > 0 && categoryUpdateReq.getParentId() != 0) {
				throw new ResException("该分类有子分类，不能修改为二级分类");
			}
		}
		category.setId(categoryUpdateReq.getCategoryId());
		category.setName(categoryUpdateReq.getCategoryName());
		category.setStyle(categoryUpdateReq.getStyle());
		category.setSort(categoryUpdateReq.getSort());
		category.setDescription(categoryUpdateReq.getDescription());
		category.setParentId(categoryUpdateReq.getParentId());
		category.setUpdateBy(UserContextHolder.getUserId());
		category.setUpdateTime(new Date());
		categoryMapper.updateById(category);
	}

	@Override
	public List<CategoryVO> getSubCategoryList(Integer parentId) {
		List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
				.eq(Category::getParentId, parentId)
				.orderByAsc(Category::getSort));
		return CategoryMapConstruct.INSTANCE.toCategoryVOList(categories);
	}
}




